package com.sky.sky_server.controller.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sky.sky_common.result.Result;
import com.sky.sky_pojo.dto.DefaultAddressDTO;
import com.sky.sky_pojo.entity.AddressBook;
import com.sky.sky_server.service.impl.AddressBookServiceImpl;

import lombok.extern.slf4j.Slf4j;

@RestController("userAddressBookController")
@RequestMapping("/user/addressBook")
@Slf4j
public class AddressBookController {
    
    @Autowired
    private AddressBookServiceImpl addressBookServiceImpl;

    /**
     * 添加地址簿
     * @param adressBook
     * @return
     */
    @PostMapping
    public Result addAddressBook(@RequestBody AddressBook adressBook) {
        log.info("添加地址簿 {}", adressBook);
        int res = addressBookServiceImpl.addAddressBook(adressBook);
        if(res >= 0)
            return Result.success("添加成功");
        return Result.error("添加失败");
    }

    /**
     * 查看地址簿
     */
    @GetMapping("/list")
    public Result list() {
        return Result.success(addressBookServiceImpl.list());
    }

    /**
     * 查看默认地址簿
     * @return
     */
    @GetMapping("/default")
    public Result defaultAddressBook() {
        return Result.success(addressBookServiceImpl.defaultAdressBook());
    }

    /**
     * 根据id修改地址簿
     * @param addressBook
     * @return
     */
    @PutMapping
    public Result updateAddressBook(@RequestBody AddressBook addressBook) {
        return Result.success(addressBookServiceImpl.updateAddressBook(addressBook));
    }

    /**
     * 修改默认地址簿
     * @param id
     * @return
     */
    @PutMapping("/default")
    public Result updateDefaultAddressBook(@RequestBody DefaultAddressDTO defaultAddressDTO) {
        Long id = defaultAddressDTO.getId();
        return Result.success(addressBookServiceImpl.updateDefaultAddressBook(id));
    }

    /**
     * 根据id查询地址簿
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id) {
        return Result.success(addressBookServiceImpl.getById(id));
    }

    /**
     * 根据id删除地址簿
     */
    @DeleteMapping
    public Result delete(@RequestParam Long id) {
        return Result.success(addressBookServiceImpl.delete(id));
    }


}
